		cd "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files"
		
*Load data on eudcation level for all relevant years
	odbc load, exec("select Sun2000niva, p_id=Person_LopNr from P0624_Lisa_1992") dsn("P0624") clear
gen year=1992
		save temp, replace
		
		foreach year in 1995 1999 2003 2007 2011{
		odbc load, exec("select Sun2000niva, p_id=Person_LopNr from P0624_Lisa_`year'") dsn("P0624") clear
		gen year =`year'
		append using temp
		save temp, replace
		}

		*Dedine years of education
	replace Sun2000niva="" if Sun2000niva=="*" | Sun2000niva=="-"
	destring Sun2000niva, replace force
	replace  Sun2000niva=. if Sun2000niva>=999

gen educ_year=.
replace educ_year= 7.5 if Sun2000niva>=100 & Sun2000niva<200
replace educ_year= 9.4 if Sun2000niva>=200 & Sun2000niva<300
replace educ_year= 11.2 if Sun2000niva>=300 & Sun2000niva<330
replace educ_year= 12.4 if Sun2000niva>=330 & Sun2000niva<400
replace educ_year= 14.2 if Sun2000niva>=410& Sun2000niva<530
replace educ_year= 17 if Sun2000niva>=530 & Sun2000niva<600
replace educ_year= 20.4 if Sun2000niva>=600 & Sun2000niva<=1000

drop Sun

***Add data on birthyear, drop those younger than 18 years old
joinby p_id using "Birthyear_sex.dta"
		ren FodelseAr birth_year
		gen age = year-birth_year
		gen woman= Kon==2
		drop if age <18
		drop   birth_year  Kon
		
		
	drop if year==.
	
	**Add earnings score
			joinby p_id using "income residual new.dta", unmatched(master)
		
		drop _merge
			 
			 
	**Add and clean enlistment data		 
			 joinby p_id using "enlistment base file.dta", unmatched(master)
		
		drop _merge
			 
	ren pprf_befl leader

	ren pprf_pgrp iq

drop  pprf_pf 
replace iq=. if iq==0
replace leader=. if leader==0

** Add data on birth region			 
joinby p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\Birth_region.dta", unmatched(master) _merge(_merge)
drop FodelseVarldsdel _merge

***Add file with political data

joinby p_id year using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\ENGLISH politicians pol 1988-2011 limited.dta", unmatched(master) _merge(_merge)
drop _merge

***Add file with parents income percentile in 1979
	cd "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Figures population comparisons\Parents-politicians comparison"
			joinby p_id using "Parents income 1979.dta", unmatched(master)
			
***Add file with parents socioeconomic status in 1980, clean data		
		cd "\\micro.intra\Projekt\P0624$\P0624_Gem\Political dynasties\Occupational following"	
		drop _merge
	joinby p_id using "sei parents 1980.dta", unmatched(master)
	replace sei_father=int(sei_father/10)
replace sei_father=sei_father+1 if sei_father>3 & sei_father!=.
replace sei_father=4 if sei_father==9


*** Create log file used for Table 1 and Table B3	
	log using table_1, replace
	sum woman age m_fborn iq leader inc_res educ_year parpct_father parpct_mother dum_sei_1- dum_sei_6 if nom!=1 & elec_parl!=1
		sum woman age m_fborn iq leader inc_res educ_year parpct_father parpct_mother dum_sei_1- dum_sei_6 if nom==1 & elected!=1
		sum woman age m_fborn iq leader inc_res educ_year parpct_father parpct_mother dum_sei_1- dum_sei_6 if elected==1
		sum woman age m_fborn iq leader inc_res educ_year parpct_father parpct_mother dum_sei_1- dum_sei_6 if chair_app==1		
		sum woman age m_fborn iq leader inc_res educ_year parpct_father parpct_mother dum_sei_1- dum_sei_6 if elec_parl==1

		tab sei_father if nom!=1 & elec_parl!=1
		tab sei_father  if nom==1 & elected!=1
		tab sei_father  if elected==1
		tab sei_father  if chair_app==1		
		tab sei_father  if elec_parl==1
		
		log close
		